package com.farm.area.controller;

import com.farm.area.pojo.FarmLand;
import com.farm.area.service.FarmLandDeviceService;
import com.farm.constant.MessageConstant;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.shop.feign.ShopFeign;
import com.farm.shop.pojo.CustDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈哲
 * @create 2020/10/16 10:36
 */
@RestController
@CrossOrigin
@RequestMapping("/farmLandDevice")
public class FarmLandDeviceController {

    @Autowired
    private ShopFeign shopFeign;

    @Autowired
    private FarmLandDeviceService farmLandDeviceService;
    /**
     * 巡检(根据大田id查询所有设备的设备编号，运行状态，安装状态，位置)
     * @param farmlandId
     * @return
     */
    @RequestMapping("/inspection")
    public Result inspection(@RequestParam("farmlandId") Integer farmlandId){
        List<CustDevice> list = shopFeign.findCustDevicesByfarmlandId(farmlandId);
        if(list == null || list.size() == 0){
            return new Result(false, StatusCode.ERROR, MessageConstant.SELECT_DEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_DEVICE_SUCCESS,list);
    }

    /**
     * 开启设备
     * @param cdid
     * @return
     */
    @RequestMapping("/start/{cdid}")
    public Result start(@PathVariable("cdid") Integer cdid){
        int count = farmLandDeviceService.start(cdid);
        if(count == 0){
            return new Result(false,StatusCode.ERROR,MessageConstant.START_DEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.START_DEVICE_SUCCESS);
    }

    /**
     * 关闭设备
     * @param cdid
     * @return
     */
    @RequestMapping("/close/{cdid}")
    public Result close(@PathVariable("cdid") Integer cdid){
        int count = farmLandDeviceService.close(cdid);
        if(count == 0){
            return new Result(false,StatusCode.ERROR,MessageConstant.CLOSE_DEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.CLOSE_DEVICE_SUCCESS);
    }

    /**
     * 大田基本信息
     * @param farmlandId
     * @return
     */
    @RequestMapping("/basicInformationOfFarm")
    public Result basicInformationOfFarm(@RequestParam("farmlandId") Integer farmlandId){
        FarmLand farmLand = farmLandDeviceService.basicInformationOfFarm(farmlandId);
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARMlAND_SUCCESS,farmLand);
    }
}
